//
//  XUITabBarViewController.m
//  nav
//
//  Created by wjxfhxy on 15-6-10.
//  Copyright (c) 2015年 wjxfhxy. All rights reserved.
//

#import "XUITabBarViewController.h"

#import "UIViewExtend.h"

#import "XUINavigationController.h"

#import "UIViewController+XUIViewConroller.h"

#import "UIViewController+XUINavigationController.h"

@interface XUITabBarViewController ()<UITabBarDelegate>
{
    NSMutableArray *_viewControllers;
    
    XUITabBar *_tabBar;
}

@end

@implementation XUITabBarViewController

- (void)dealloc
{
    [_tabBar release_mm];
    [_viewControllers removeAllObjects];
    [_viewControllers release_mm];
    superdealloc_mm;
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor blueColor];
    
    self.hidesBackButton = YES;
    
    _viewControllers = [NSMutableArray new];
    
    _tabBar = [[XUITabBar alloc]initWithFrame:CGRectZero];
    _tabBar.delegate = self;
    [_tabBar sizeToFit];
    _tabBar.top = self.view.height - _tabBar.height;
    [self.view addSubview:_tabBar];
}

- (void)viewWillLayoutSubviews
{
    _tabBar.top = self.view.height - _tabBar.height;
    [self createDefaultIndexOfViewControllerClass];
}

-(void)createDefaultIndexOfViewControllerClass
{
    if(_viewControllers.count == 0)
    {
        NSUInteger n = [self numberOfSwitchViewController];
        if(n <= 0)
            return;
        for(NSInteger i = 0;i < n;i++)
            [_viewControllers addObject:[NSNull null]];
        
        NSUInteger defaultIndex = [self defaultCreateIndexOfViewControllerClass];
        
        [self createViewControllerClassAtIndex:defaultIndex block:^(Class cls,XUIViewControllerSwitchParam* param)
        {
            UIViewController *viewController = [[[cls alloc] initWithViewControllerSwitchParam:param] autorelease_mm];
            viewController.subVclDelegate = self;
            
            _tabBar.selectedItem = _tabBar.items[defaultIndex];
            
            [_viewControllers replaceObjectAtIndex:defaultIndex withObject:viewController];
            [self.view insertSubview:viewController.view belowSubview:_tabBar];
            
            [self updateViewControllerNavigationItem:viewController.navigationItem];
            
            [viewController viewControllerTapAppear];
        }];
    }
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(_viewControllers.count <= 0)
        return;
    
    NSUInteger tag = item.tag;

    UIViewController *viewController = _viewControllers[tag];
    if([viewController isKindOfClass:[NSNull class]])
    {
        [[self topViewController] viewControllerTapDisappear];
        
        [self createViewControllerClassAtIndex:tag block:^(Class cls,XUIViewControllerSwitchParam* param)
        {
            UIViewController *viewController = [[[cls alloc] initWithViewControllerSwitchParam:param] autorelease_mm];
            viewController.subVclDelegate = self;
            
            [_viewControllers replaceObjectAtIndex:tag withObject:viewController];
            [self.view insertSubview:viewController.view belowSubview:_tabBar];
            
            [self updateViewControllerNavigationItem:viewController.navigationItem];
            
            [viewController viewControllerTapAppear];
        }];
    }
    else if([self topViewController] == viewController)
    {
        [viewController viewControllerTapAppeared];
    }
    else
    {
        [[self topViewController] viewControllerTapDisappear];
        
        [self.view insertSubview:viewController.view belowSubview:_tabBar];
        
        [self updateViewControllerNavigationItem:viewController.navigationItem];
        
        [viewController viewControllerTapReAppear];
    }
}

- (NSUInteger)numberOfSwitchViewController
{
    return _tabBar.items.count;
}

-(void)selectViewControllerAtIndex:(NSUInteger)selectIndex
{
    _tabBar.selectedItem = _tabBar.items[selectIndex];
    
    [self tabBar:_tabBar didSelectItem:_tabBar.selectedItem];
}

- (void)updateViewControllerNavigationItem:(UINavigationItem*)navigationItem
{
    UINavigationItem *item = self.navigationItem;
    
    item.hidesBackButton = navigationItem.hidesBackButton;
    
    item.title = navigationItem.title;
    
    item.titleView = navigationItem.titleView;
    
    item.backBarButtonItem = navigationItem.backBarButtonItem;
    
    item.leftBarButtonItems = navigationItem.leftBarButtonItems;
    
    item.rightBarButtonItems = navigationItem.rightBarButtonItems;
}

@end
